#encoding=utf-8
'''
Created on 2011-11-20

@author: guanyw

python version:2.6

'''
import re
import string
from symbol import return_stmt


def sentence_to_triple_list(sents):
    sent_list = sents.split(' ')
    wd_list = [wd for idx,wd in enumerate(sent_list) if idx%3==0]
    pos_list = [wd for idx,wd in enumerate(sent_list) if idx%3==1]
    ne_list = [wd for idx,wd in enumerate(sent_list) if idx%3==2]
    return zip(wd_list,pos_list,ne_list)

def merge_ne(sent_triple):
    ne_list = []
    temp = ''
    for wd,pos,ne in sent_triple:
        if ne.startswith('/B') or ne.startswith('/I'):
            temp += wd
        elif ne.startswith('/E'):
            temp += wd
            ne_list.append(temp)
            temp =''
        elif ne.startswith('/S'):
            ne_list.append(wd)
    return  ne_list

def untag_sentence(sents):
    return re.sub('\s*/[a-zA-Z\-]+\s*','',sents)


def year_translate(s):
    trs_table =u'零一二三四五六七八九'
    y = re.findall('\d{4}', s)
    if y:
        yt = ''.join([trs_table[int(c)] for c in y[0]])
        s = re.sub(y[0], yt, s)
    
    return (s,len(re.findall('['+trs_table+']{4}', s)))


def rep_num(s):
    trs_table =(u'１２３４５６７８９０．％',u'1234567890.%')
    for ix,src in enumerate(trs_table[0]):
        s = s.replace(src,trs_table[1][ix])
    return s     

def trans_num_tozh(s):
    ys,yf = year_translate(s)
    if yf :return ys
    

def translate_number(s,tag):
    
    ys,yf = year_translate(s)
    if yf:
        return ys
    
    if tag not in ['n','m'] or re.findall('[百千万亿]', s) or s.startswith('第') or s.startswith('周'):
        return s

    Num={u'一':'1',u'二':'2',u'两':'2',u'三':'3',u'四':'4',
         u'五':'5',u'六':'6',u'七':'7',u'八':'8',u'九':'9',
         u'十':'',u'零':'0'}

    result_str = ''
    
    for i,c in enumerate(s): 
        if c == '十' and (not i or s[i-1] not in Num.keys()):
            if i < len(s)-1 and s[i+1] not in Num.keys() or i == len(s)-1:
                result_str += '10'
            else:
                result_str += '1'
        else:
            result_str += Num[c] if c in Num.keys() else c
#        
#        if c in Num.keys() and cnt <= 1:
#            if c=='十' and result!=0:
#                result*=10
#                cnt = 0
#            else:
#                cnt += 1
#                result+=int(Num[c])
#        else:
#            result_str += str(result) if result else ''
#            result = 0
#            cnt  = 0
#            result_str += c
#    result_str += str(result) if result else ''
    
    s = result_str
    return s

if __name__ == '__main__':
    pass